package ru.picman;

import ru.picman.util.GreenThread;

/**
 * With this class you are allowed to create an animation that
 * basically is the sprite of the entity specified, but then it's
 * flashing. Simply construct the flasher for the desired entity
 * and add the thread to the {@link GameLoop}
 * 
 * @author Matthijs
 */
public class BirdFlasher 
			 extends GreenThread
{
	
	/**
	 * Constructs the BirdFlasher. The bird will flash for the 
	 * specified duration. The state of the bird will be set to 
	 * immortal when the animation starts and set to normal when 
	 * it ends.
	 * 
	 * @param bird The bird to flash
	 * @param duration The flashing duration
	 */
	public BirdFlasher(Bird bird, long duration) {
		if (bird == null || duration <= flash_interval)
			throw new IllegalArgumentException("entity should be non-null and duration > flash_interval ("+flash_interval+")");
		this.bird = bird;
		this.duration = duration;
	}

	
    public void run() {
		duration -= flash_interval;
		if (duration < 0) {
			bird.setImmortal(false);
			bird.setVisible(true);
			stop();
			return;
		}
		bird.setImmortal(true);
		bird.setVisible(!bird.isVisible());
		sleep(flash_interval);
    }
	
	//
	
	private final Bird bird;
	
	private long duration;
	
	private static final int flash_interval = 250;
	
}